package com.example.lenovo.zyfypt_329zay.model;




import com.example.lenovo.zyfypt_329zay.bean.TwareBean;
import com.example.lenovo.zyfypt_329zay.bean.VideoBean;
import com.example.lenovo.zyfypt_329zay.iface.TwareListener;
import com.example.lenovo.zyfypt_329zay.iface.Twareiface;
import com.example.lenovo.zyfypt_329zay.iface.VideoListener;
import com.example.lenovo.zyfypt_329zay.iface.Videoiface;
import com.example.lenovo.zyfypt_329zay.service.TwareService;
import com.example.lenovo.zyfypt_329zay.service.VideoService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 大玉儿 on 2017/4/20.
 */
//model 获取文章数据
public class TwareModel implements Twareiface {
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    //构造函数
    public TwareModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //获取文章数据
    @Override
    public void getResultList(String tware, int page, String sessionID, final TwareListener twareListener) {
        //使用Retrofit-2
        TwareService twareService=retrofit.create(TwareService.class);
        Call<List<TwareBean>> call
                =twareService.getResult(tware,page,sessionID);
        //使用Retrofit-3
        call.enqueue(new Callback<List<TwareBean>>() {
            @Override
            public void onResponse(Call<List<TwareBean>> call, Response<List<TwareBean>> response) {
                if(response.isSuccessful()&&response!=null){
                    twareListener.onResponse(response.body());
                }
                else
                    twareListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<TwareBean>> call, Throwable t) {
                twareListener.onFail(t.toString());
            }
        });
    }
}
